home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / Preferences.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-26  |  6.4 KB  |  219 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11. #include    <Limits.h>
  12.  
  13. #ifndef __ICAPI__
  14. #include <ICAPI.h>
  15. #endif
  16.  
  17. #include    "UtilsSys7.h"
  18. #include    "Conversions.h"
  19. #include    "MovableModal.h"
  20. #include    "FabLibResIDs.h"
  21. #include    "SimResIDs.h"
  22.  
  23. #include    "Disasm.h"
  24. #include    "DoEditDialog.h"
  25. #include    "DoMenu.h"
  26. #include    "DragManSim.h"
  27. #include    "Dump.h"
  28. #include    "Globals.h"
  29. #include    "Preferences.h"
  30. #include    "Main.h"
  31. #include    "Registers.h"
  32. #include    "GrowZone.h"
  33.  
  34. #if defined(FabSystem7orlater)
  35.  
  36. static Boolean PrefsPreProcessKey(EventRecord *thEv, DialogPtr theD);
  37. static pascal Boolean PrefsFilter(DialogPtr theD, EventRecord *thEv, short *iHit);
  38. static void SetTopLeft(Point *thePt, WindowPtr w);
  39.  
  40. #pragma segment Rare
  41.  
  42. enum {
  43. kItemRememberWindPos = 3,
  44. kItemContinuousDumpScroll,
  45. kItemInfLoopsDetect,
  46. kItemInitialPCVal,
  47. kItemInitialSPVal,
  48. kItemStackSize
  49. };
  50.  
  51. void Preferences(void)
  52. {
  53. Str255    InitPCStr, InitSPStr, StackSizeStr;
  54. long    dummy;
  55.  
  56. dialogItems    things[] = {{ ok, 0, 1L },
  57.                         { cancel, 0, 0L },
  58.                         { kItemRememberWindPos, 0, 0L },
  59.                         { kItemContinuousDumpScroll, 0, 0L },
  60.                         { kItemInfLoopsDetect, 0, 0L },
  61.                         { kItemInitialPCVal, 0, 0L },
  62.                         { kItemInitialSPVal, 0, 0L },
  63.                         { kItemStackSize, 0, 0L },
  64.                         { 0, 0, 0L}
  65.                         };
  66.  
  67. things[kItemRememberWindPos-1].refCon = gPrefs.remembWind;
  68. things[kItemContinuousDumpScroll-1].refCon = gPrefs.NeXTScroll;
  69. things[kItemInfLoopsDetect-1].refCon = gPrefs.infLoopsDetect;
  70. things[kItemInitialPCVal-1].refCon = (long)&InitPCStr;
  71. things[kItemInitialSPVal-1].refCon = (long)&InitSPStr;
  72. things[kItemStackSize-1].refCon = (long)&StackSizeStr;
  73.  
  74. ShortToHexString(gPrefs.DefPCValue, InitPCStr);
  75. ShortToHexString(gPrefs.DefSPValue, InitSPStr);
  76. MyNumToString(gPrefs.DefStkSize, StackSizeStr);
  77.  
  78. if (HandleMovableModalDialog(things, gPrefs.remembWind ? &gPrefs.generalPrefsTL : nil, nil, nil, nil, nil, nil,
  79.     AdjustMenus,
  80.     Handle_My_Menu,
  81.     DomyKeyEvent,
  82.     PrefsPreProcessKey,
  83.     nil,
  84.     DoUpdate,
  85.     DoActivate,
  86.     DoHighLevelEvent,
  87.     DoOSEvent,
  88.     DoIdle,
  89.     ULONG_MAX,
  90.     kDLOG_Prefs) == ok) {
  91.     gPrefs.remembWind = things[kItemRememberWindPos-1].refCon;
  92.     gPrefs.NeXTScroll = things[kItemContinuousDumpScroll-1].refCon;
  93.     gPrefs.infLoopsDetect = things[kItemInfLoopsDetect-1].refCon;
  94.     HexStringToShort(InitPCStr, (short *)&gPrefs.DefPCValue);
  95.     HexStringToShort(InitSPStr, (short *)&gPrefs.DefSPValue);
  96.     StringToNum(StackSizeStr, &dummy);
  97.     gPrefs.DefStkSize = dummy;
  98.     }
  99. }
  100.  
  101. Boolean PrefsPreProcessKey(EventRecord *thEv, DialogPtr theD)
  102. {
  103. short    iHit;
  104. unsigned char    keypressed;
  105. Boolean    result = true;
  106.  
  107. keypressed = CHARFROMMESSAGE(thEv->message);
  108. if ((keypressed >= 'a') && (keypressed <= 'z')) {
  109.     keypressed -= 'a' - 'A';    /* cambiare! */
  110.     CHARFROMMESSAGE(thEv->message) = keypressed;
  111.     }
  112. iHit = ((DialogPeek)theD)->editField + 1;
  113. if (keypressed >= 32 && ((thEv->modifiers & cmdKey) == 0)) {
  114.     switch (iHit) {
  115.         case kItemInitialPCVal:
  116.         case kItemInitialSPVal:
  117.             result = ( Munger((Handle)GetString(kSTR_HEXALLOWED), 1L, &keypressed,
  118.                             1L, 0L, 0L) >= 0L );
  119.             break;
  120.         case kItemStackSize:
  121.             result = ( Munger((Handle)GetString(kSTR_DECALLOWED), 1L, &keypressed,
  122.                             1L, 0L, 0L) >= 0L );
  123.             break;
  124.         }
  125.     }
  126. return result;
  127. }
  128.  
  129. #pragma segment CleanUp
  130.  
  131. /* SavePreferencesFile: in the end we save our gPrefs */
  132.  
  133. void SavePreferencesFile(void)
  134. {
  135. ParamBlockRec    myPB;
  136. EventRecord    dummyEv;
  137. FSSpec    myFSS;
  138. Handle    myStrHand;
  139. short    prefsFRefNum;
  140. Boolean    targetFolder, isAnAlias;
  141. OSErr    err;
  142. SignedByte    oldState;
  143.  
  144. oldState = WantThisHandleSafe(myStrHand = (Handle)GetString(kSTR_PREFSFILENAME));
  145. if ((err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  146.             &myFSS.vRefNum, &myFSS.parID)) == noErr) {
  147.     err = FSMakeFSSpecCompat(myFSS.vRefNum, myFSS.parID, (ConstStr255Param)*myStrHand, &myFSS);
  148.     if (err == noErr || err == fnfErr) {
  149.         if (err == fnfErr)
  150.             err = FSpCreateCompat(&myFSS, '????', kPreferencesFolderType, smCurrentScript);
  151.         if (err == noErr)
  152.             if ((err = ResolveAliasFile(&myFSS, true, &targetFolder, &isAnAlias)) == noErr)
  153.                 if (targetFolder)
  154.                     err = paramErr;
  155.                 else
  156.                     if ((err = FSpOpenDFCompat(&myFSS, fsWrPerm, &prefsFRefNum)) == noErr) {
  157.                         SetTopLeft(&gPrefs.AnimTopLeft, gWPtr_Animation);
  158.                         SetTopLeft(&gPrefs.RegsTopLeft, gWPtr_Registers);
  159.                         gPrefs.RegsBase = GetRegistersBase();
  160.                         GetWindowUserState(gWPtr_IO, &gPrefs.IOUserState);
  161.                         SetTopLeft(&gPrefs.MProgTopLeft, gWPtr_Microprogram_Ed);
  162.                         SetTopLeft(&gPrefs.DumpTopLeft, gWPtr_Dump);
  163.                         gPrefs.DumpHeight = PRCT_B(gWPtr_Dump) - PRCT_T(gWPtr_Dump);
  164.                         gPrefs.DumpScrollVal = GetDumpVScrollValue();
  165.                         SetTopLeft(&gPrefs.DisasmTopLeft, gWPtr_Disasm);
  166.                         gPrefs.DisasmHeight = PRCT_B(gWPtr_Disasm) - PRCT_T(gWPtr_Disasm);
  167.                         gPrefs.DisasmScrollVal = GetDisasmVScrollValue();
  168.                         gPrefs.AnimVisible = ((WindowPeek)gWPtr_Animation)->visible;
  169.                         gPrefs.RegsVisible = ((WindowPeek)gWPtr_Registers)->visible;
  170.                         gPrefs.IOVisible = ((WindowPeek)gWPtr_IO)->visible;
  171.                         gPrefs.DumpVisible = ((WindowPeek)gWPtr_Dump)->visible;
  172.                         gPrefs.DisasmVisible = ((WindowPeek)gWPtr_Disasm)->visible;
  173.                         myPB.ioParam.ioCompletion = 0L;
  174.                         myPB.ioParam.ioRefNum = prefsFRefNum;
  175.                         myPB.ioParam.ioBuffer = (Ptr)&gPrefs;
  176.                         myPB.ioParam.ioReqCount = sizeof(struct myprefs);
  177.                         myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
  178.                         myPB.ioParam.ioPosOffset = 0L;
  179.                         (void)PBWriteAsync(&myPB);
  180.                         while (myPB.ioParam.ioResult > 0) {
  181.                             SystemTask();
  182.                             (void)EventAvail(everyEvent, &dummyEv);
  183.                             }
  184.                         (void)FSClose(prefsFRefNum);
  185.                         if ((err = myPB.ioParam.ioResult) == noErr)
  186.                             err = AddSTRRes2Doc(&myFSS, '????', kPreferencesFolderType,
  187.                                                 kSTR_NOOPENORPRINT, smSystemScript);
  188.                         }
  189.         }
  190.     }
  191. HSetState(myStrHand, oldState);
  192. }
  193.  
  194. /* SetTopLeft: common routine which calculates the topLeft coordinate
  195. of a GrafPort’s portRect in global coordinates;
  196. it _changes_ the current port */
  197.  
  198. static void SetTopLeft(Point *thePt, GrafPtr w)
  199. {
  200. *thePt = topLeft(w->portRect);
  201. SetPort(w);
  202. LocalToGlobal(thePt);
  203. }
  204.  
  205. void CleanUp(void)
  206. {
  207. CleanupGrowZone();
  208. if (gDragManagerActive) {
  209.     MyRemoveHWindow(gWPtr_Registers);
  210.     MyRemoveHWindow(gWPtr_Disasm);
  211.     MyRemoveHWindow(gWPtr_Dump);
  212.     }
  213. if (gICinst)
  214.     (void)ICStop(gICinst);
  215. }
  216.  
  217. #endif
  218.  
  219.